﻿using System;
using App.Model;
using App.Model.Main;
using FreeSql;


namespace App.DBAdmin
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("库表管理工具，此程序将同步表结构、表数据");

            //var currDbConn = "Data Source=182.92.126.137;Port=3306;User ID=root;Password=wx19930918;Database=com_main;Charset=utf8;Max pool size=100;Allow User Variables=True;SslMode=none;";

            ////同步当前项目库表结构
            //using (var dbc = new AppMainContext(DataType.MySql,
            //   currDbConn))
            //{
            //    dbc.Orm.CodeFirst.SyncStructure<SysParam>();
            //}

            ////同步表数据
            //SyncTableData<SysParam>(
            //    "server=182.92.126.137;uid=sa;pwd=xzzhxy@zz123;database=DCMain;pooling=true;min pool size=1;max pool size=100;connect timeout=20;",
            //    DataType.SqlServer,
            //    currDbConn,
            //    DataType.MySql);

            //同步表数据2
            SyncTableData<SysParam>(
                "Data Source=182.92.126.137;Port=3506;User ID=root;Password=wx19930918;Database=com_main;Charset=utf8;Max pool size=100;Allow User Variables=True;SslMode=none;",
                DataType.MySql,
                "server=127.0.0.1;uid=sa;pwd=wx;database=app_main;pooling=true;min pool size=1;max pool size=100;connect timeout=20;",
                DataType.SqlServer);

        }





        /// <summary>
        /// 将其他库数据向当前项目导入
        /// </summary>
        /// <typeparam name="TTable"></typeparam>
        /// <param name="sConnStr">源库连接</param>
        /// <param name="sDbType">源库类型</param>
        /// <param name="dConnStr"></param>
        /// <param name="dDbType"></param>
        public static void SyncTableData<TTable>(string sConnStr, DataType sDbType, string dConnStr, DataType dDbType)
            where TTable : EntityBase<int>, new()
        {
            //源数据库 操作完请注释掉
            var fsqlSourceDb = new FreeSql.FreeSqlBuilder()
                    .UseConnectionString(sDbType, sConnStr)
                    .UseNameConvert(FreeSql.Internal.NameConvertType.PascalCaseToUnderscoreWithLower)               //表名和字段名使用user_name这种格式
                    .UseAutoSyncStructure(false)
                    .UseNoneCommandParameter(true)
                    //.UseLazyLoading(false)      //导航属性懒加载
                    .UseMonitorCommand(DbContextPool.ExecuteSqlBefore, DbContextPool.ExecuteSqlAfter)
                    //.UseConfigEntityFromDbFirst(true)
                    .Build();

            //当前项目库dbc
            using (var dbc = new AppMainContext(dDbType, dConnStr))
            {
                //通用的表数据同步
                //var sList = fsqlSourceDb.Select<TTable>().ToList();
                //对特殊表进行排序处理
                var sList = fsqlSourceDb.Select<SysParam>().Where(w => w.Type > 0).OrderBy(f => f.Type).ToList();
                //foreach (var item in sList)
                //{
                //    dbc.Insert(item);
                //}
                dbc.InsertList(sList);
            }

            fsqlSourceDb.Dispose();
        }

    }
}
